<script>
import { add } from 'script-ext-html-webpack-plugin/lib/custom-attributes'

export default {
  name: 'buildingForm',
  data() {
    return {
      /* 表单数据对象 */
      formData: {
        // 楼宇名称
        name: '',
        // 层数
        floors: '',
        // 在管面积
        area: '',
        // 物业费单价
        propertyFeePrice: ''
      },
      /* 表单规则对象 */
      formRules: {
        name: [{ required: true, message: '请输入楼宇名称', trigger: 'blur' }],
        floors: [{ required: true, message: '请输入楼宇层数', trigger: 'blur' }],
        area: [{ required: true, message: '请输入在管面积', trigger: 'blur' }],
        propertyFeePrice: [{ required: true, message: '请输入所需物业费', trigger: 'blur' }]
      }
    }
  },
  methods: {
    /* 添加楼宇的回调函数 */
    async doAddBuilding(formData) {
      try {
        const addResult = await this.$store.dispatch('buildingStore/addBuilding', formData)
        this.$message.success(addResult)
        this.$emit('success')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 编辑楼宇的回调函数 */
    async doEditBuilding(formData) {
      try {
        const editResult = await this.$store.dispatch('buildingStore/editBuilding', formData)
        this.$message.success(editResult)
        this.$emit('success')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 查询楼宇的回调函数 */
    async findBuilding(buildingId) {
      try {
        const { id, name, floors, area, propertyFeePrice } = await this.$store.dispatch('buildingStore/findBuilding', buildingId)
        Object.assign(this.formData, { id, name, floors, area, propertyFeePrice })
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 重置表单的回调函数 */
    resetFormData() {
      this.formData.id = ''
      this.$refs.formRef.resetFields()
    },
    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      const valid = await this.$refs.formRef.validate().catch(error => error)
      if (!valid) return
      if (this.formData.id) {
        this.doEditBuilding(this.formData)
      } else {
        this.doAddBuilding(this.formData)
      }
    }
  },
  emits: ['success', 'cancel']
}
</script>

<template>
  <el-form label-width="100px" :rules="formRules" :model="formData" ref="formRef">
    <el-form-item label="楼宇名称" prop="name">
      <el-input size="small" placeholder="请输入楼宇名称" v-model="formData.name"></el-input>
    </el-form-item>
    <el-form-item label="楼宇层数" prop="floors">
      <el-input size="small" placeholder="请输入楼宇层数" v-model="formData.floors"></el-input>
    </el-form-item>
    <el-form-item label="在管面积" prop="area">
      <el-input size="small" placeholder="请输入在管面积" v-model="formData.area">
        <template slot="append">m²</template>
      </el-input>
    </el-form-item>
    <el-form-item label="所需物业费" prop="propertyFeePrice">
      <el-input size="small" placeholder="请输入所需物业费" v-model="formData.propertyFeePrice">
        <template slot="append">元/m²</template>
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="danger" size="small" @click="$emit('cancel')">取消</el-button>
      <el-button type="primary" size="small" @click="handleSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss">
.el-form {
  width: 80%;
}
</style>
